//
//  StatusCell.swift
//  仿一个新浪微博
//
//  Created by 孙家林 on 15/9/22.
//  Copyright © 2015年 sunjialin. All rights reserved.
//

import UIKit

//控件间距
let statusCellControlMargin: CGFloat = 8.0

//cell标识符枚举

enum StatusCellIdentifier: String {

    case NormalCell = "NormalCell"
    
    case ForwordCell = "ForwordCell"
    
    /// 静态函数
    /// 根据微博数据，返回对应的标示符
    static func cellId(status: Status) -> String {
    
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwordCell.rawValue
    }
}


class StatusCell: UITableViewCell {
    
    //微博数据模型
    var status: Status? {
    
        didSet {
            
            topView.status = status
            contentLabel.text = status?.text ?? ""
            
            //设置配图数据
            pictureView.status = status
            
            //设置配图视图尺寸
            pictureWidthCons?.constant = pictureView.bounds.size.width
            pictureHeightCons?.constant = pictureView.bounds.size.height
            
            pictureTopCons?.constant = (pictureView.bounds.size.height == 0) ? 0 : statusCellControlMargin
        }
    }
    //图片宽度约束
    var pictureWidthCons: NSLayoutConstraint?
    //图片高度约束
    var pictureHeightCons: NSLayoutConstraint?
    
    //图片顶部约束
    var pictureTopCons: NSLayoutConstraint?
    
    //计算行高
    func rowHeight(status: Status) -> CGFloat {
        //设置属性
        self.status = status
        //强行更新布局，所有布局的frame都会发生改变
        //使用自动布局，不要直接修改frame
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    
    //界面设置
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() {
        
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        //顶部视图
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        
        //内容标签
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: statusCellControlMargin, y: statusCellControlMargin))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2 * statusCellControlMargin))
        
        //配图视图
//        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: statusCellControlMargin))
//        pictureWidthCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        pictureHeightCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//        pictureTopCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Top)
        
        //底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -statusCellControlMargin, y: statusCellControlMargin))
        
        //底部约束
//        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
    }
    
    
    //懒加载控件
    private lazy var topView: StatusTopView = StatusTopView()
    //内容标签
    lazy var contentLabel: UILabel = {
        
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        
        label.numberOfLines = 0
        
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * statusCellControlMargin
        
        return label
    }()
    
    //配图视图
    lazy var pictureView: StatusPictureView = StatusPictureView()
    
    //底部视图
    lazy var bottomView: StatusBottomView = StatusBottomView()
    

}
